package org.mat.ipaas.ucenter.bff.admin.modules.system;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.mat.framework.lang.dto.MatApiResponse;
import org.mat.ipaas.ucenter.service.api.system.LoginRestAPI;
import org.mat.ipaas.ucenter.service.constant.CommonConstant;
import org.mat.ipaas.ucenter.service.dto.system.entity.SysUserDTO;
import org.mat.ipaas.ucenter.service.dto.system.model.SysLoginModelDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Author scott
 * @since 2018-12-17
 */
@RestController
@RequestMapping("/sys")
@Api(tags = "用户登录")
@Slf4j
public class LoginController {

    @Autowired
    private LoginRestAPI loginRestAPI;

    @ApiOperation("登录接口")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public MatApiResponse<JSONObject> login(@RequestBody SysLoginModelDTO sysLoginModel) {
        MatApiResponse<JSONObject> matApiResponse = loginRestAPI.login(sysLoginModel);
        return matApiResponse;
    }

    /**
     * 【vue3专用】获取用户信息
     */
    @GetMapping("/user/getUserInfo")
    public MatApiResponse<JSONObject> getUserInfo(HttpServletRequest request) {
        String accessToken = request.getHeader("X-Access-Token");
        String version = request.getHeader(CommonConstant.VERSION);
        MatApiResponse<JSONObject> matApiResponse = loginRestAPI.getUserInfo(accessToken, version);
        return matApiResponse;

    }

    /**
     * 退出登录
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/logout")
    public MatApiResponse<Object> logout(HttpServletRequest request, HttpServletResponse response) {
        MatApiResponse<Object> matApiResponse = loginRestAPI.logout();
        return matApiResponse;
    }

    /**
     * 获取访问量
     *
     * @return
     */
    @GetMapping("loginfo")
    public MatApiResponse<JSONObject> loginfo() {
        MatApiResponse<JSONObject> matApiResponse = loginRestAPI.loginfo();
        return matApiResponse;
    }

    /**
     * 获取访问量
     *
     * @return
     */
    @GetMapping("visitInfo")
    public MatApiResponse<List<Map<String, Object>>> visitInfo() {
        MatApiResponse<List<Map<String, Object>>> matApiResponse = loginRestAPI.visitInfo();
        return matApiResponse;
    }


    /**
     * 登陆成功选择用户当前部门
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/selectDepart", method = RequestMethod.PUT)
    public MatApiResponse<JSONObject> selectDepart(@RequestBody SysUserDTO user) {
        MatApiResponse<JSONObject> matApiResponse = loginRestAPI.selectDepart(user);
        return matApiResponse;
    }

    /**
     * 短信登录接口
     *
     * @param jsonObject
     * @return
     */
    @PostMapping(value = "/sms")
    public MatApiResponse<String> sms(@RequestBody JSONObject jsonObject) {
        MatApiResponse<String> matApiResponse = loginRestAPI.sms(jsonObject);
        return matApiResponse;
    }


    /**
     * 手机号登录接口
     *
     * @param jsonObject
     * @return
     */
    @ApiOperation("手机号登录接口")
    @PostMapping("/phoneLogin")
    public MatApiResponse<JSONObject> phoneLogin(@RequestBody JSONObject jsonObject) {
        MatApiResponse<JSONObject> matApiResponse = loginRestAPI.phoneLogin(jsonObject);
        return matApiResponse;
    }

    /**
     * 获取加密字符串
     *
     * @return
     */
    @GetMapping(value = "/getEncryptedString")
    public MatApiResponse<Map<String, String>> getEncryptedString() {
        MatApiResponse<Map<String, String>> matApiResponse = loginRestAPI.getEncryptedString();
        return matApiResponse;
    }

    /**
     * 后台生成图形验证码 ：有效
     *
     * @param key
     */
    @ApiOperation("获取验证码")
    @GetMapping(value = "/randomImage/{key}")
    public MatApiResponse<String> randomImage(@PathVariable("key") String key) {
        MatApiResponse<String> matApiResponse = loginRestAPI.randomImage(key);
        return matApiResponse;
    }

    /**
     * 切换菜单表为vue3的表
     */
    @RequiresRoles({"admin"})
    @GetMapping(value = "/switchVue3Menu")
    public MatApiResponse<String> switchVue3Menu() {
        MatApiResponse<String> matApiResponse = loginRestAPI.switchVue3Menu();
        return matApiResponse;
    }

    /**
     * app登录
     *
     * @param sysLoginModel
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/mLogin", method = RequestMethod.POST)
    public MatApiResponse<JSONObject> mLogin(@RequestBody SysLoginModelDTO sysLoginModel) throws Exception {
        MatApiResponse<JSONObject> matApiResponse = loginRestAPI.mLogin(sysLoginModel);
        return matApiResponse;
    }

    /**
     * 图形验证码
     *
     * @param sysLoginModel
     * @return
     */
    @RequestMapping(value = "/checkCaptcha", method = RequestMethod.POST)
    public MatApiResponse<?> checkCaptcha(@RequestBody SysLoginModelDTO sysLoginModel) {
        MatApiResponse<?> matApiResponse = loginRestAPI.checkCaptcha(sysLoginModel);
        return matApiResponse;
    }

    /**
     * 登录二维码
     */
    @ApiOperation(value = "登录二维码", notes = "登录二维码")
    @GetMapping("/getLoginQrcode")
    public MatApiResponse<?> getLoginQrcode() {
        MatApiResponse<?> matApiResponse = loginRestAPI.getLoginQrcode();
        return matApiResponse;
    }

    /**
     * 扫码二维码
     */
    @ApiOperation(value = "扫码登录二维码", notes = "扫码登录二维码")
    @PostMapping("/scanLoginQrcode")
    public MatApiResponse<?> scanLoginQrcode(@RequestParam String qrcodeId, @RequestParam String token) {
        MatApiResponse<?> matApiResponse = loginRestAPI.scanLoginQrcode(qrcodeId, token);
        return matApiResponse;
    }


    /**
     * 获取用户扫码后保存的token
     */
    @ApiOperation(value = "获取用户扫码后保存的token", notes = "获取用户扫码后保存的token")
    @GetMapping("/getQrcodeToken")
    public MatApiResponse getQrcodeToken(@RequestParam String qrcodeId) {
        MatApiResponse matApiResponse = loginRestAPI.getQrcodeToken(qrcodeId);
        return matApiResponse;
    }

}